<template>
  <div class="rate">
    <div class="fanhui">
      <i class="iconfont icon-fanhui" @click="backs"></i>
    </div>
    <div class="header">
      <div class="header_left">{{ shopInfo.score }}</div>
      <div class="header_right">
        <div class="right_title">商家评分</div>
        <div class="right_rateScore">
          <el-rate v-model="shopInfo.score" disabled text-color="#ff9900" score-template="{shopInfo.score}"></el-rate>
        </div>
      </div>
    </div>
    <div class="info">
      <div class="info_header">
        <div class="info_header_tag" @click="tagActive(1)" :class="active == 1 ? 'info_header_tag_active' : ''">
          <span>全部</span>
          <span>{{ goodRateList.length + badRateList.length }}</span>
        </div>
        <div class="info_header_tag" @click="tagActive(2)" :class="active == 2 ? 'info_header_tag_active' : ''">最新</div>
        <div class="info_header_tag" @click="tagActive(3)" :class="active == 3 ? 'info_header_tag_active' : ''">
          <span>好评</span>
          <span>{{ goodRateList.length }}</span>
        </div>
        <div class="info_header_tag" @click="tagActive(4)" :class="active == 4 ? 'info_header_tag_active' : ''">
          <span>差评</span>
          <span>{{ badRateList.length }}</span>
        </div>
      </div>
      <div class="info_rate" v-for="(item, index) in rateList" :key="index">
        <div class="rate_user">
          <div class="user_left">
            <img src="../../img/匿名头像.jpg" alt="" v-if="item.isHide == 1" />
            <el-image :src="item.user.avatar" fit="cover" v-if="item.isHide == 0"></el-image>
          </div>
          <div class="user_right">
            <div class="right_name" v-if="item.isHide == 0">{{ item.user.name }}</div>
            <div class="right_name" v-if="item.isHide == 1">匿名用户</div>
            <div class="right_date">{{ item.rateTime }}</div>
          </div>
        </div>
        <div class="rate_text">
          <div class="text_score">
            <el-rate v-model="item.rateScore" show-text text-color="#ff9900" :texts="texts" disabled></el-rate>
          </div>
          <div class="text_info">{{ item.rateText }}</div>
          <div class="text_img" v-if="item.rateImgList.length != 0">
            <el-image :src="it.rateImage" fit="cover" v-for="(it, ind) in item.rateImgList" :key="ind"></el-image>
          </div>
          <div class="text_return" v-if="item.rateReturn != '' && item.rateReturn != null">
            <div class="return_title">商家回复</div>
            <div class="return_text">{{ item.rateReturn }}</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      shopId: '',
      active: 1,
      texts: ['非常差', '差', '一般', '满意', '非常满意'],
      rateList: [],
      goodCount: 0,
      badCount: 0,
      shopInfo: [],
      goodRateList: [],
      badRateList: [],
      sortRateList: [],
    };
  },

  created() {
    this.shopId = this.$route.query.shopId;
    this.getRateList();
    this.getShopInfo();
  },

  methods: {
    async getShopInfo() {
      const { data: res } = await this.$http.get('shop/getById/' + this.shopId);
      if (res.code == 1) {
        this.shopInfo = res.data;
        return;
      } else {
        return this.$message.error('信息获取失败');
      }
    },
    async getRateList() {
      var getParams = { shopId: this.shopId, dishId: '' };
      const { data: res } = await this.$http.get('rate/getAllRate', { params: getParams });
      if (res.code == 1) {
        this.rateList = res.data;
        this.sortRateList = res.data;
        for (let i = 0; i < this.rateList.length; i++) {
          if (this.rateList[i].isGood == 1) {
            this.goodRateList.push(this.rateList[i]);
          }
          if (this.rateList[i].isGood == 0) {
            this.badRateList.push(this.rateList[i]);
          }
        }
      } else {
        return this.$message.error('评价信息获取失败');
      }
    },
    backs() {
      window.sessionStorage.removeItem('activePath');
      this.$router.push('/takeOut');
    },
    tagActive(index) {
      this.active = index;
      if (index == 1) {
        this.rateList = this.sortRateList.sort(function (a, b) {
          let obj1 = a['rateTime'];
          let obj2 = b['rateTime'];
          const val1 = Math.floor(new Date(obj1).getTime() / 1000);
          const val2 = Math.floor(new Date(obj2).getTime() / 1000);
          return val1 - val2;
        });
      }
      if (index == 2) {
        this.rateList = this.sortRateList.sort(function (a, b) {
          let obj1 = a['rateTime'];
          let obj2 = b['rateTime'];
          const val1 = Math.floor(new Date(obj1).getTime() / 1000);
          const val2 = Math.floor(new Date(obj2).getTime() / 1000);
          return val2 - val1;
        });
      }
      if (index == 3) {
        this.rateList = this.goodRateList;
        return;
      }
      if (index == 4) {
        this.rateList = this.badRateList;
        return;
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.rate {
  background-color: #f4f4f4;
  height: calc(100vh - 273px);
  width: 100%;
  position: absolute;

  .fanhui {
    .icon-fanhui {
      color: #f9fafa;
      font-size: 25px;
      margin-left: 10px;
      position: absolute;
      top: -230px;
    }
  }
  .header {
    height: 70px;
    display: flex;
    align-items: center;
    background-color: #fefefe;
    .header_left {
      color: #f88112;
      font-size: 35px;
      font-weight: bold;
      margin-left: 25px;
      margin-right: 10px;
    }
    .header_right {
      .right_title {
        color: #a8a8a8;
        font-size: 15px;
        margin-bottom: 5px;
      }
    }
  }
  .info::-webkit-scrollbar {
    display: none;
  }
  .info {
    background-color: #fefefe;
    margin-top: 10px;
    padding: 15px;
    overflow: auto;
    height: calc(100vh - 384px);
    scrollbar-width: none;
    -ms-overflow-style: none;
    .info_header {
      display: flex;
      align-items: center;
      height: 40px;
      .info_header_tag {
        padding: 4px 8px 4px 8px;
        background-color: #fdf7df;
        margin-right: 10px;
        border-radius: 5px;
        font-size: 13px;
        span:nth-child(1) {
          margin-right: 5px;
        }
      }
      .info_header_tag_active {
        background-color: #fdcc31;
        color: #000000;
      }
    }
    .info_rate {
      margin-top: 20px;
      border-bottom: 1px solid #f4f4f4;
      .rate_user {
        display: flex;
        align-items: center;
        margin-bottom: 15px;
        .user_left {
          :deep(.el-image) {
            height: 40px;
            width: 40px;
            border-radius: 50px;
            margin-right: 10px;
          }
          img {
            height: 40px;
            width: 40px;
            border-radius: 50px;
            margin-right: 10px;
          }
        }
        .user_right {
          .right_name {
            color: #000000;
            font-size: 16px;
            font-weight: bold;
          }
          .right_date {
            font-size: 13px;
            color: #b0b0b0;
            margin-bottom: 5px;
          }
        }
      }
      .rate_text {
        .text_score {
          margin-bottom: 5px;
          :deep(.el-rate__text) {
            color: #9c9c9d !important;
          }
        }
        .text_info {
          font-size: 16px;
          color: #505050;
          letter-spacing: 2px;
          margin-bottom: 20px;
        }
        .text_img {
          display: flex;
          flex-wrap: wrap;
          width: 100%;
          margin-bottom: 20px;
          :deep(.el-image) {
            height: 150px;
            width: 150px;
            border-radius: 10px;
            margin-right: 10px;
            margin-top: 5px;
          }
        }
        .text_return {
          padding: 10px;
          background-color: #f9f9f9;
          border-radius: 10px;
          margin-bottom: 20px;
          .return_title {
            color: #818183;
            font-size: 15px;
            font-weight: bold;
            letter-spacing: 1px;
            margin-bottom: 5px;
          }
          .return_text {
            font-size: 14px;
            color: #818183;
            letter-spacing: 2px;
          }
        }
      }
    }
    .info_rate:last-child {
      border-bottom: none;
    }
  }
}
</style>